package com.lenovo.agricultureclient.request;

import org.json.JSONException;
import org.json.JSONObject;

import com.lenovo.agricultureclient.util.Util;

/**
 * 控制器请求类
 * @author zongbingwu
 *
 */
public class ControlRequest extends AgricultureRequest 
{
	//action name
	private static final String ACTION = "control"; 
	
	//水泵
	private int waterPump = 0; //0表示关闭，1表示打开
	//风扇
	private int blower = 0;//0表示关闭，1表示打开
	//路灯
	private int roadlamp = 0;//0表示关闭，1表示打开
	//蜂鸣器
	private int buzzer = 0;//0表示关闭，1表示打开
	
	private boolean isCtrlWater = false;//是否要控制水泵
	private boolean isCtrlBlower = false;//是否要控制风扇
	private boolean isCtrlRoadlamp = false;//是否要控制路灯
	private boolean isCtrlBuzzer = false;//是否要控制蜂鸣器

	@Override
	protected String getActionName() {
		return ACTION;
	}
	
	//jason协议时的获取body函数
	@Override
	protected String onGetJasonBody() 
	{
		JSONObject jsonObj = new JSONObject();
		try 
		{
			//是否要控制风扇
			if(isCtrlBlower){
				jsonObj.put("Blower", blower);
			}
			//是否要控制蜂鸣器
			if(isCtrlBuzzer){
				jsonObj.put("Buzzer", buzzer);
			}
			//是否要控制路灯
			if(isCtrlRoadlamp){
				jsonObj.put("Roadlamp", roadlamp);
			}
			//是否要控制水泵
			if(isCtrlWater){
				jsonObj.put("WaterPump", waterPump);
			}
			return jsonObj.toString();
		} 
		catch (JSONException e) {
			e.printStackTrace();
		}
		return super.onGetJasonBody();
	}
	
	//soap协议时的获取body函数
	@Override
	protected String onGetSoapBody() 
	{
		String soapBody = "";
		//是否要控制风扇
		if(isCtrlBlower){
			soapBody += Util.getXmlElementStr("Blower", blower+"");
		}
		//是否要控制蜂鸣器
		if(isCtrlBuzzer){
			soapBody += Util.getXmlElementStr("Buzzer", buzzer+"");
		}
		//是否要控制路灯
		if(isCtrlRoadlamp){
			soapBody += Util.getXmlElementStr("Roadlamp", roadlamp+"");
		}
		//是否要控制水泵
		if(isCtrlWater){
			soapBody += Util.getXmlElementStr("WaterPump", waterPump+"");
		}
		return Util.getSoapXml(soapBody);
	}

	public void setBlower(int blower) {
		this.blower = blower;
		this.isCtrlBlower = true;
	}
	
	public void setBuzzer(int buzzer) {
		this.buzzer = buzzer;
		this.isCtrlBuzzer = true;
	}

	public void setWaterPump(int waterPump) {
		this.waterPump = waterPump;
		this.isCtrlWater = true;
	}

	public void setRoadlamp(int roadlamp) {
		this.roadlamp = roadlamp;
		this.isCtrlRoadlamp = true;
	}

	//控制状态重置
	public void resetRequest()
	{
		isCtrlWater = false;
		isCtrlBlower = false;
		isCtrlRoadlamp = false;
		isCtrlBuzzer = false;
	}

}
